package com.naihe.anderson.sms.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.naihe.anderson.sms.R;

public class PullToRefreshListView extends ListView implements OnScrollListener{

	private int firstVisibleItem;
  
    private OnRefreshListener mOnRefreshListener;
    private OnScrollListener mOnScrollListener;
    
    private LayoutInflater mInflater;  
    private RelativeLayout mRefreshView;  
    private ProgressBar mRefreshViewProgress;
    
    private int mCurrentScrollState;  
    
    private int mRefreshViewHeight = 60;  
    private float mLastMotionY;  

    private boolean hasHistoryData;
    private boolean mRefreshState;
    
	public PullToRefreshListView(Context context) {  
        super(context);  
        init(context);
    }  
  
    public PullToRefreshListView(Context context, AttributeSet attrs) {  
        super(context, attrs);
        init(context);
    }  
  
    public PullToRefreshListView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);
        init(context);
    }  
	
    private void init(Context context) {  

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  
        mRefreshView = (RelativeLayout) mInflater.inflate(R.layout.talkmns_message_headview, this, false);  
        mRefreshViewProgress = (ProgressBar) mRefreshView.findViewById(R.id.reloading_progress);  
  
        addHeaderView(mRefreshView);
        
        hasHistoryData = false;
        mRefreshState = false;
        super.setOnScrollListener(this);
    } 

	public boolean isHasHistoryData() {
		return hasHistoryData;
	}

	public void setHasHistoryData(boolean hasHistoryData) {
		this.hasHistoryData = hasHistoryData;
	}

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {  
        mOnRefreshListener = onRefreshListener;  
    }  
    
    @Override  
    public void setOnScrollListener(AbsListView.OnScrollListener l) {  
        mOnScrollListener = l;  
    } 
    
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
        switch (event.getAction()) {
        	case MotionEvent.ACTION_UP:
        		if(mRefreshView.getHeight() > mRefreshViewHeight && mOnRefreshListener != null){
                	if(hasHistoryData){
                		mRefreshView.setPadding( mRefreshView.getPaddingLeft(), 10, mRefreshView.getPaddingRight(), 0);
                		mRefreshState = true;
                		mRefreshViewProgress.setVisibility(View.VISIBLE);
                		onRefresh();
                	} else {
                		resetHeaderPadding();
                	}
               } else {
            	   resetHeaderPadding();
               }
               break;  
            case MotionEvent.ACTION_DOWN:  
                mLastMotionY = event.getY();  
                break;  
            case MotionEvent.ACTION_MOVE:
            	if(!mRefreshState){
            		final float preY = mLastMotionY; 
            		float nowY = event.getY();  
            		int deltaY = (int) ((nowY - preY - mRefreshViewHeight) / 1.7);
            		if(deltaY < 150){
            			mRefreshView.setPadding( mRefreshView.getPaddingLeft(), deltaY,  
            					mRefreshView.getPaddingRight(), mRefreshView.getPaddingBottom());
            		}
            	}
                break;  
        }  
        return super.onTouchEvent(event);  
    } 
    
    public void onRefresh() {  
        if (mOnRefreshListener != null) {  
            mOnRefreshListener.onRefresh();
        }
    }
    
    public interface OnRefreshListener {
        public void onRefresh();  
    }

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		mCurrentScrollState = scrollState;
		
		if (mOnScrollListener != null) {  
            mOnScrollListener.onScrollStateChanged(view, scrollState);  
        }
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		this.firstVisibleItem = firstVisibleItem;
		if(firstVisibleItem == 0 && mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL){
			mRefreshViewProgress.setVisibility(View.INVISIBLE);
		}
		if (mOnScrollListener != null) {  
            mOnScrollListener.onScroll(view, firstVisibleItem,  
                    visibleItemCount, totalItemCount);  
        } 
	}
	
	public int getFirstVisibleItem() {
		return firstVisibleItem;
	}

	public void resetHeaderPadding() {
		mRefreshState = false;
        mRefreshView.setPadding(mRefreshView.getPaddingLeft(), 0, mRefreshView.getPaddingRight(), 0);
        mRefreshViewProgress.setVisibility(View.GONE);
        if(hasHistoryData){
        	mRefreshViewHeight = mRefreshView.getHeight();
        }
    }

	public boolean ismRefreshState() {
		return mRefreshState;
	}

	public void setmRefreshState(boolean mRefreshState) {
		this.mRefreshState = mRefreshState;
	} 
}
